<script src="<?php echo $base_url?>js/jquery-1.4.4.js" type="text/javascript"></script>
<script type="text/javascript">
function funViewCla(classified) {
	window.location='index.php?classified='+classified;
}
function funDelCla(postId) {
	var delPro=confirm("Are you sure want to delete this classified?");
	if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("delCla").value=postId;
		frm.submit();
	}	
}

</script>

<script src="<?php echo $base_url?>js/jquery-1.4.4.js" type="text/javascript"></script>
<script>
function onlyint(event) {
	
	var charCode = event.keyCode || event.which;
	if(event.shiftKey && charCode == 9)
	{return true}
	else  if (event.shiftKey)
	{ $('#digit_err').show().fadeOut("slow"); return false; }
	else if  (((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 46)) )
	{ return true; } else { $('#digit_err').show().fadeOut("slow"); return false; }
}
</script>
<?php
require_once "../includes/pagination.php";

if(!empty($_POST['delCla']))
{
	$claId=$_POST['delCla'];
	$sqlDelMem="Delete from tbl_post where post_id=$claId";
	$rstDelMem=mysql_query($sqlDelMem);	
}

	$itemPerPage=15;

	if(isset($_POST['btnSearchPro'])) {
		$searchKey = $_POST['searchkey'];		
		$searchBy = $_POST['searchby'];		
		$date = $_POST['dYear']."-".$_POST['dMonth']."-".$_POST['dDay'];
		if(!empty($_POST['postedBy'])){
			
		$sql_shocat = "SELECT tbl_post.*, tbl_users.username, tbl_users.user_id 
						  FROM tbl_post, tbl_users 
						  WHERE tbl_users.username LIKE '%".$_POST['postedBy']."%' AND
						  tbl_users.user_id=tbl_post.posted_by";
						  
		if(!empty($_POST['productName']))
			$sql_shocat .= " AND tbl_post.post_name LIKE '%".$_POST['productName']."%'";			        
		if(isset($_POST['priceFrom']) && !empty($_POST['priceTo']))
			$sql_shocat .= " AND tbl_post.price BETWEEN ".$_POST['priceFrom']." AND ".$_POST['priceTo'];
		if($_POST['dYear']!="" && $_POST['dMonth']!="" && $_POST['dDay']!="") {
		 		$date=$_POST['dYear']."-".$_POST['dMonth']."-".$_POST['dDay'];
				$sql_shocat .= " AND tbl_post.posted_date LIKE '$date%'";				
		 }	  
		} 
		else if(!empty($_POST['productName']) || (isset($_POST['priceFrom']) || !empty($_POST['priceTo'])) || (!empty($_POST['dYear'])&& !empty($_POST['dMonth']) && !empty($_POST['dDay']))){
			$sql_shocat = "SELECT * FROM tbl_post WHERE ";		
		if(!empty($_POST['productName']))
			$sql_shocat .= "post_name LIKE '%".$_POST['productName']."%' AND ";			        
		if(!empty($_POST['priceFrom']) && !empty($_POST['priceTo']))
			$sql_shocat .= "price BETWEEN ".$_POST['priceFrom']." AND ".$_POST['priceTo']."  AND";
		else if(!empty($_POST['priceFrom']))
			$sql_shocat .= "price >=".$_POST['priceFrom']."  AND";
		else if(!empty($_POST['priceTo']))
			$sql_shocat .= "price <=".$_POST['priceTo']."  AND";
		if($_POST['dYear']!="" && $_POST['dMonth']!="" && $_POST['dDay']!="") {
		 		$date=$_POST['dYear']."-".$_POST['dMonth']."-".$_POST['dDay'];
				$sql_shocat .= "posted_date LIKE '$date%' AND ";				
		 }	  
	$count=strlen($sqlSelPro);
        $sql_shocat=substr($sql_shocat,0,($count-4));
	}	
	else {
		if(!empty($_GET['sort']))
		{
			$sql_shocat="SELECT * FROM tbl_post";
		}
		else{
			$sql_shocat="SELECT * FROM tbl_post ORDER BY post_id DESC";
		}
	}
	} 
	else {
		if(!empty($_GET['sort']))
		{
			$sql_shocat="SELECT * FROM tbl_post";
		}
		else{
			$sql_shocat="SELECT * FROM tbl_post ORDER BY post_id DESC";
		}
	}


//$cond = '';
//$sort = '';
//$ut = $u = $fn = $ln = $dg = 'asc';
//if(isset($_GET['sort']))
//{
//	if($_GET['sort']=='post_name')
//	{
//		if($_GET['s_type']=='asc')
//		{	$cond=' ORDER BY post_name ASC';	$sort = '&sort=post_name&s_type=asc';	$ut='desc';	}
//		else
//		{	$cond=' ORDER BY post_name DESC';	$sort = '&sort=post_name&s_type=desc';	}
//	}
//	else if($_GET['sort']=='posted_date')
//	{
//		if($_GET['s_type']=='asc')
//		{	$cond=' ORDER BY posted_date ASC';	$sort = '&sort=posted_date&s_type=asc'; $dg='desc'; }
//		else
//		{	$cond=' ORDER BY posted_date DESC';	$sort = '&sort=posted_date&s_type=desc';}
//	}
//}

?>
<form name="form1" id="form1" method="post" action="">
 <input type="hidden" name="delCla" id="delCla">
			  
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Classified List</td>
  </tr>
  <tr>
    <td height="577">
	<table width="100%">
            <tbody>
		<tr>
			<td>
			<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr><td height="10"></td><td></td><td></td><td></td><td></td></tr>
					<tr>
						<td width="2%" height="31"></td>
						<td width="16%">Clasified Name</td>
						<td width="30%"><input type="text" name="productName" id="productName" style="width:157px;" value="<?=stripslashes($_POST['productName'])?>"  /></td>
						<td width="13%">Posted by</td>
						<td width="39%"><input type="text" name="postedBy" id="postedBy" style="width:215px;" value="<?=stripslashes($_POST['postedBy'])?>" /></td>
					</tr>
					<tr>
						<td height="34">&nbsp;</td>
						<td>Price range</td>
						<td>
							<input type="text" name="priceFrom" id="priceFrom" style="width:67px;" onkeydown='return onlyint(event);' value="<?=stripslashes($_POST['priceFrom'])?>" /> &nbsp;&nbsp;
							<input type="text" name="priceTo" id="priceTo" style="width:67px;" onkeydown='return onlyint(event);'  value="<?=stripslashes($_POST['priceTo'])?>"/>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<font color="gray">From</font>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<font color="gray">To</font><br />
							<div style="text-align: center; color: red; display: none;" id="digit_err">Digits  Only</div>
							<?php /*
							<select name="priceFrom" id="priceFrom" style="width:78px;">
								<option value="">From</option>
								<?php
								$i=0;
								for($i=0; $i<=5000; $i+=100) {
								echo "<option value='$i'> $$i </option>";
								}
								?>
							</select>
							    
							
							
							<select name="priceTo" id="priceTo"  style="width:78px;">
								<option value="">To</option>
								<?php
								$i=100;
								for($i=100; $i<=5000; $i+=100) {
								echo "<option value='$i'> $$i </option>";
								}
								?>
							</select>  */
							?>
						</td>
						<td>Posted date</td>
						<td>
							<select name="dMonth" id="dMonth" style="width:80px">
								<option value="">..Month..</option>
								<option value="01" <?php if($_POST['dMonth']=="01") { echo 'selected'; } ?>>January</option>
								<option value="02" <?php if($_POST['dMonth']=="02") { echo 'selected'; } ?>>February</option>
								<option value="03" <?php if($_POST['dMonth']=="03") { echo 'selected'; } ?>>March</option>
								<option value="04" <?php if($_POST['dMonth']=="04") { echo 'selected'; } ?>>April</option>
								<option value="05" <?php if($_POST['dMonth']=="05") { echo 'selected'; } ?>>May</option>
								<option value="06" <?php if($_POST['dMonth']=="06") { echo 'selected'; } ?>>June</option>
								<option value="07" <?php if($_POST['dMonth']=="07") { echo 'selected'; } ?>>July</option>
								<option value="08" <?php if($_POST['dMonth']=="08") { echo 'selected'; } ?>>August</option>
								<option value="09" <?php if($_POST['dMonth']=="09") { echo 'selected'; } ?>>September</option>
								<option value="10" <?php if($_POST['dMonth']=="10") { echo 'selected'; } ?>>October</option>
								<option value="11" <?php if($_POST['dMonth']=="11") { echo 'selected'; } ?>>November</option>
								<option value="12" <?php if($_POST['dMonth']=="12") { echo 'selected'; } ?>>December</option>
							</select>
							<select name="dDay" id="dDay" style="width:80px">
								<option value="">..Day..</option>
								<?php
								for($i=1;$i<=31;$i++) {
									if($i<=9)
									$date = '0'.$i;
									else
									$date = $i;
									?>
									<option value="<?php echo $date;?>" <?php if($_POST['dDay']==$date) { echo 'selected'; }?>><?php echo $date;?></option>
									<?php
								}
								?>
								
							</select>
							<input type="dYear" name="dYear" id="emailAgain3" value="YYYY" onclick="this.value=''" size="4" maxlength="4" value="<?=$_POST['dYear']?>"/>
						</td>
					</tr>
					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Classified" /></td>
					</tr>
				</table>
			</form>
			</td>
		</tr>
<!--		<tr>
			<td>
				<b>Sort  By :</b> 	<a href="<?php echo $base_url;?>admin/index.php?classified&sort=post_name&s_type=<?php echo $ut;?>">Name</a> &nbsp;&nbsp;&nbsp;
						<a href="<?php echo $base_url;?>admin/index.php?classified&sort=posted_date&s_type=<?php echo $dg;?>">Date Posted.</a>
			</td>
		</tr>
-->        </tbody></table>
	<br>
	<table width="100%"  border="0" cellspacing="0">      
      <tr class="InnerHeader">       
        <td width="16%" class="line">Name</td>
		<td width="14%" class="line">Posted By</td>
        <td width="13%" class="line">Price</td>
        <td width="17%" class="line">Post Date</td>
        <td width="7%" class="line">&nbsp;</td> 
		<td width="10%" class="line">&nbsp;</td>         
      </tr>
      <?php 				 
	
	//$itemPerPage=15;
	//$search='';
	//$sql_shocat="SELECT * FROM tbl_post";
	$rst_shocat=mysql_query(getPagingQuery($sql_shocat,$itemPerPage)) or die(mysql_error());				 
	
	//echo $sql_shocat;
	$numRows = mysql_num_rows($rst_shocat);
	if($numRows>0) {				
		while($row_shocat = mysql_fetch_array($rst_shocat)) {				 
		$post_id=$row_shocat['post_id'];
		$name=stripslashes($row_shocat['post_name']);
		$selUsers=mysql_fetch_array(mysql_query("select * from tbl_users where user_id='".$row_shocat['posted_by']."'"));
		$posted_by = $selUsers['username'];
		$price=stripslashes($row_shocat['price']);
		$post_date=$row_shocat['posted_date'];

		echo "<tr>";
		echo "<td class='line'>$name</td>";
		echo "<td class='line'>$posted_by</td>";
		echo "<td class='line'>$price</td>";
		echo "<td class='line'>$post_date</td>";
		echo "<td height='20' class='line'><input type='button' name='view' value='View' class='button'  onclick='funViewCla($post_id)'></td> ";
		echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='funDelCla($post_id)';></td> ";
		echo "</tr>";				
		}
	} else {
	       echo "<tr>
			       <td colspan='6' class='line'>No Classified found.</td>
			 </tr>";
	}				 
		?>      
       </table>
	<div align="center">
		<div align="center"><?php
								$link="classified";
								echo $disp=getPagingLink($sql_shocat,$itemPerPage,$link);							   
								?>
							</div>
	</td>
 </tr>
</table>